package com.gps.utilities;

import java.util.HashMap;
import java.util.Locale;
import android.content.Context;
import android.media.AudioManager;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.Engine;

public class TTS {

	TextToSpeech tts = null;
	Context c = null;
	private boolean phoneOnCall;
	private boolean mute = false;
	HashMap<String, String> ttsParams = new HashMap<String, String>();
	private AudioManager am = null;
	int i = 0;

	public TTS(Context c) {
		this.c = c;
		am = (AudioManager) (c.getSystemService(Context.AUDIO_SERVICE));

		tts = new TextToSpeech(c, new TextToSpeech.OnInitListener() {

			public void onInit(int status) {
				if (status != TextToSpeech.ERROR) {
					tts.setLanguage(Locale.US);
					tts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {
						public void onUtteranceCompleted(String utteranceId) {
							if (mute) {
								am.setStreamMute(AudioManager.STREAM_MUSIC,
										false);
								mute = false;
							}

						}
					});
				}
			}
		});

		ttsParams.put(TextToSpeech.Engine.KEY_PARAM_STREAM, ""
				+ AudioManager.STREAM_NOTIFICATION);
	}

	public void gpsSignal(boolean avilable) {
		String text = "GPS signal is " + ((avilable) ? "available" : "lost");
		speakThis(text);
	}

	public void runningStatus(String speed, double distance, String calories,
			long time, boolean ghost) {
		String text = "";
		if (!ghost) {
			text = "your running time is " + retrunTime(time)
					+ ", your corrent speed is " + speed + ", you have passed "
					+ returnDistance(distance) + ", and burned about "
					+ calories + " calories";
		} else {
			text = "the opponent running time is " + retrunTime(time)
					+ ", hes current speed is " + speed + ", and passed "
					+ returnDistance(distance);
		}
		speakThis(text);

	}

	private String returnDistance(double distance) {

		int intDistance = (int) distance;

		if (distance > 1000) {
			return (intDistance / 1000) + "kilometers " + intDistance % 1000
					+ "meters ";
		} else
			return Math.round(distance) + "meters ";

	}

	private String retrunTime(long time) {
		time = time / 1000;
		if (time < 60)
			return time + "seconds ";
		long seconds = time % 60;
		time = time / 60;
		if (time < 60)
			return time + "minutes and " + seconds + "seconds ";
		long minutes = time % 60;
		time = time / 60;
		return time + "hours and " + minutes + "minutes ";
	}

	public void whoIsfirst(boolean you) {
		String text = "";

		if (you)
			text = "you have just passed your opponent";
		else
			text = "attention!, the opponent has passed you";

		speakThis(text);

	}

	private void speakThis(String text) {
		boolean music = false;
		int musicVol = 0;

		if (am.getMode() != AudioManager.MODE_IN_CALL) {
			if (am.isMusicActive()) {
				music = true;
				mute = true;
				am.setStreamMute(AudioManager.STREAM_MUSIC, true);
				musicVol = am.getStreamVolume(AudioManager.STREAM_MUSIC);
			}

			ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MYTTS");
			i++;
			tts.speak(text, TextToSpeech.QUEUE_ADD, ttsParams);
			if (music) {

			}
		}
	}

	public void shutDown() {
		if (tts.isSpeaking())
			tts.stop();
		tts.shutdown();
	}

	public boolean isSpeak() {
		return tts.isSpeaking();
	}

	public void finishRunning() {
		String text = "the opponent has finished activity";
		speakThis(text);
	}

}
